package fhp.hlhj.giantfold.customView

import android.animation.Animator
import android.animation.AnimatorInflater
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.view.Gravity
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.adapter.FlipAdp
import fhp.hlhj.giantfold.interfaces.IFlip
import fhp.hlhj.giantfold.javaBean.BaseBean
import fhp.hlhj.giantfold.javaBean.FlipBean
import fhp.hlhj.giantfold.presenter.FlipPresenter
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.MyUtils
import kotlinx.android.synthetic.main.flip_dialog.*
import java.util.*

/**
 * author: FHP
 * data: on 2018\1\30 0030 9:57
 * E-Mail: 103945089@qq.com
 */
class FlipDialog(context: Context,private val orderStr:String,private val onFinishFlip: OnFinishFlip) :Dialog(context, R.style.CustomDialog), IFlip {
    private lateinit var flipAdp:FlipAdp
    private lateinit var presenter:FlipPresenter
    private val datas=ArrayList<String>()
    private var isReady=false
    private lateinit var tittle:TextView
    private var type=-1
    private lateinit var out:Animator
    private lateinit var ain:Animator
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.flip_dialog)
        this.window.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT)
        presenter= FlipPresenter(this)
        /*使能动画*/
        presenter.setAnima()


        btDismiss.setOnClickListener {
            dismiss()
        }
    }



    override fun setAni() {
        for (i in 1..6){
            datas.add("s")
        }
        flipAdp=FlipAdp(R.layout.flip_dialog_item,datas)
        recyclerview.layoutManager=GridLayoutManager(context,3)
        recyclerview.adapter=flipAdp
        btStartFlip.setOnClickListener {
            val distance = 16000
            val scale = context.resources.displayMetrics.density * distance
            for (i in 0..5){
                val animaIn = AnimatorInflater.loadAnimator(context, R.animator.anim_in)
                val animaOut = AnimatorInflater.loadAnimator(context, R.animator.anim_out)

                val flipBack = flipAdp.getViewByPosition(recyclerview,i, R.id.flipBack)
                val flipFront = flipAdp.getViewByPosition(recyclerview,i, R.id.flipFront)
                flipFront!!.cameraDistance = scale
                flipBack!!.cameraDistance = scale
                animaOut.setTarget(flipFront)
                animaIn.setTarget(flipBack)
                animaOut.start()
                animaIn.start()
            }
            btStartFlip.isClickable=false

            Thread(Runnable {
                val random = Random()
                Thread.sleep(1200)
                var i=0
                while (i<15){
                    i++
                    Thread.sleep(200)
                    recyclerview.post {
                        LogUtil.log("移动")
                        var a = random.nextInt(5)
                        val b = random.nextInt(5)
                        while (a==b){
                            a=random.nextInt(5)
                        }
                        flipAdp.notifyItemMoved(a, b)
                    }
                }
                isReady=true
            }).start()
        }


        /*条目点击事件*/
        flipAdp.setOnItemClickListener { adapter, view, position ->
            LogUtil.log("点击子布局")
            if (isReady){
                isReady=false
                val flipBack = flipAdp.getViewByPosition(recyclerview,position, R.id.flipBack)
                val flipFront = flipAdp.getViewByPosition(recyclerview,position, R.id.flipFront)
                ain = AnimatorInflater.loadAnimator(context, R.animator.anim_in)
                out = AnimatorInflater.loadAnimator(context, R.animator.anim_out)
                out.setTarget(flipBack)
                ain.setTarget(flipFront)


                /*改变前图*/
                val imgFront = flipAdp.getViewByPosition(recyclerview,position, R.id.imgFront)
                tittle = flipAdp.getViewByPosition(recyclerview,position, R.id.tittle) as TextView

                val params = HttpParams()
                params.put("order_id",orderStr)
                params.put("type",2)
                params.put("user_id",Contents.userId)
                presenter.flip(params)


            }

        }
    }
    override fun flip(baseBean: FlipBean) {
        if (baseBean.code==200){
            when(baseBean.data.fanpai_res.toInt()){
                2->{
                    imgGet.setImageResource(R.mipmap.bj_f)
                    tittle.text="存款+10%"
                    tvResult.text="存款+10%"
                }
                1->{
                    imgGet.setImageResource(R.mipmap.bj_one)
                    tittle.text="1元存款"
                    tvResult.text="1元存款"
                }
                4->{
                    imgGet.setImageResource(R.mipmap.bj_one)
                    tittle.text="100元存款"
                    tvResult.text="100元存款"
                }
                3->{
                    imgGet.setImageResource(R.mipmap.bj_s)
                    tittle.text="年华收益+1.6%"
                    tvResult.text="年华收益+1.6%"
                }
                6->{
                    imgGet.setImageResource(R.mipmap.bj_s)
                    tittle.text="年华收益+15%"
                    tvResult.text="年华收益+15%"
                }
                5->{
                    imgGet.setImageResource(R.mipmap.bj_t)
                    tittle.text="0.18元现金"
                    tvResult.text="0.18元现金"
                }
            }

            out.start()
            ain.start()
            ain.addListener(object :Animator.AnimatorListener{
                override fun onAnimationRepeat(animation: Animator?) {

                }

                override fun onAnimationEnd(animation: Animator?) {
                    tv1.visibility= View.VISIBLE
                    tvResult.visibility=View.VISIBLE
                    imgGet.visibility=View.VISIBLE
                    ObjectAnimator.ofFloat(imgGet,"scaleX",0f,1f).setDuration(800).start()
                    ObjectAnimator.ofFloat(imgGet,"scaleY",0f,1f).setDuration(800).start()

                    imgGet.setOnClickListener {
                        tv1.visibility= View.GONE
                        tvResult.visibility=View.GONE
                        imgGet.visibility=View.GONE
                        dismiss()
                    }
                    onFinishFlip.onfinish()

                }

                override fun onAnimationCancel(animation: Animator?) {

                }

                override fun onAnimationStart(animation: Animator?) {
                }

            })

        }else{
            MyUtils.toast(baseBean.msg)
        }
    }

    interface OnFinishFlip{
        fun onfinish()
    }
}